package cn.qingyun.gis.modules.scenes.service.impl;

import cn.qingyun.gis.exception.CommonException;
import cn.qingyun.gis.modules.middle.api.standard.response.RgeoAddressVo;
import cn.qingyun.gis.modules.scenes.entity.AddrPoiGovernment;
import cn.qingyun.gis.modules.scenes.entity.PoiGovernment;
import cn.qingyun.gis.modules.scenes.entity.RgeoResult;
import cn.qingyun.gis.modules.scenes.feign.PoiDetailClientService;
import cn.qingyun.gis.modules.scenes.mapper.AddrPoiGovernmentMapper;
import cn.qingyun.gis.modules.scenes.mapper.PoiGovernmentMapper;
import cn.qingyun.gis.modules.scenes.request.RoadInfoRequest;
import cn.qingyun.gis.modules.scenes.response.RoadInfoResponse;
import cn.qingyun.gis.modules.scenes.service.IPoiGovernmentService;
import cn.qingyun.gis.modules.scenes.util.GdRgeoTool;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author guest
 * @since 2024-05-18
 */
@Slf4j
@Service
@DS("merge_government_db")
public class PoiGovernmentServiceImpl extends ServiceImpl<PoiGovernmentMapper, PoiGovernment> implements IPoiGovernmentService {

    @Resource
    private PoiGovernmentMapper poiGovernmentMapper;

    @Resource
    private AddrPoiGovernmentMapper addrPoiGovernmentMapper;

    @Resource
    private PoiDetailClientService poiDetailClientService;

    @Override
    public Integer selectZoneTypeByPoiId(String poiId) {
        QueryWrapper<PoiGovernment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gd_id",poiId).select("id","zone_code");
        PoiGovernment poiGovernment = poiGovernmentMapper.selectOne(queryWrapper);
        log.debug("poiId:{}查询的网络是否覆盖信息，{}", poiId, poiGovernment);
        if (poiGovernment==null){
            return 0;
        }
        if (!StringUtils.isEmpty(poiGovernment.getZoneCode())){
            return 1;
        }
        return 0;
    }

    @Override
    public RoadInfoResponse selectRoadInfo(RoadInfoRequest request) {
        log.info("查询路巷街门牌号入参，{}",request);
        RgeoResult rgeo = GdRgeoTool.getAddressByRgeo(request.getPoiLng(), request.getPoiLat());
        RoadInfoResponse response = new RoadInfoResponse();
        log.info("调用高德查询路巷街、门牌号入参经纬度{},{},返回结果{}", request.getPoiLng(), request.getPoiLat(), rgeo);
        if (rgeo != null) {
            if (StringUtils.isNotEmpty(rgeo.getRoad())){
                response.setHouseRoad(rgeo.getRoad());
            }
            if (StringUtils.isNotEmpty(rgeo.getRoadNo())){
                String roadNoStr = PoiProofreadServiceImpl.parseRoadNoStr(rgeo.getRoadNo());
                response.setHouseNumber(roadNoStr);
            }
        }else {
            //查询地址库的地址
            RgeoAddressVo rgeoAddressVo = poiDetailClientService.queryRgeoAddrByGdId(request.getPoiId());
            if (rgeoAddressVo!=null){
                if (StringUtils.isNotEmpty(rgeoAddressVo.getRoad())){
                    response.setHouseRoad(rgeoAddressVo.getRoad());
                }
                if (StringUtils.isNotEmpty(rgeoAddressVo.getRoadNo())){
                    response.setHouseNumber(rgeoAddressVo.getRoadNo());
                }
            }
        }
        //如果高德查询的地址为空
        log.info("查询路巷街返回{}",response);
        return response;
    }
}
